package org.pearl.spring.demo.validation;

import org.springframework.validation.FieldError;
import org.springframework.validation.MapBindingResult;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by TD on 2021/10/15
 */
public class test {
    public static void main(String[] args) {
        // 创建一个校验器
        PersonValidator personValidator = new PersonValidator();
        // 创建对象
        Person person = new Person();
        person.setAge(1000);
        Map<String, Object> map = new HashMap<>();
        // 创建结果对象
        MapBindingResult result = new MapBindingResult(map, "person");
        // 进行校验并将错误信息封装到result
        personValidator.validate(person, result);
        // 打印错误信息
        List<FieldError> fieldErrors = result.getFieldErrors();
        fieldErrors.forEach(e -> {
            System.out.println(e.getField() + e.getCode());
        });
    }
}
